package me.obstsalatt.guildera2.global;

import me.obstsalatt.guildera2.container.GPlayer;
import me.obstsalatt.guildera2.utility.Util;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;

/**
 * Created with IntelliJ IDEA.
 * User: Robin
 * Date: 14.02.14, 18:42
 * Project: GuildEra2
 * Package: me.obstsalatt.guildera2.global
 * Descriprion:
 *      Supplies information about players and their relations with guilds
 */
public class PlayerManager {
    private HashSet<GPlayer> playerList;
    private Main main;

    PlayerManager(Main main) {
        this.main = main;
        this.playerList = new HashSet<GPlayer>();
        loadPlayers();
    }

    public void registerPlayer(String name) {
        playerList.add(new GPlayer(findPlayerByName(name, false)));
        savePlayer(name);
    }

    public void savePlayer(String name) {
        GPlayer ply = getPlayer(name);
        Util.writeFile(getPlayerFilePathForPlayer(name) + "gplayer.dat", ply.toString());
    }

    public void savePlayers() {
        for(GPlayer ply : playerList) {
            Util.writeFile(getPlayerFilePathForPlayer(ply.getPlayer().getName()) + "gplayer.dat", ply.toString());
        }
    }

    public void loadPlayers() {
        String plyData = "";
        File playerFolder = new File(getPlayerFilePath());

        for(File plyFolder : playerFolder.listFiles()) {
            if(plyFolder.isDirectory()) {
                try {
                    plyData = Util.readFile(getPlayerFilePathForPlayer(plyFolder.getName()) + "gplayer.dat");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String[] segments = plyData.split("#");
                playerList.add(new GPlayer(findPlayerByName(segments[0], false), segments[1], segments[2]));
            }
        }
    }

    public String getPlayerFilePath() {
        return  (main.getDataFolder().getAbsolutePath() + File.pathSeparator + "guildera2"
                + File.pathSeparator + "player"
                + File.pathSeparator);
    }

    public String getPlayerFilePathForPlayer(String playerName) {
        return  (main.getDataFolder().getAbsolutePath() + File.pathSeparator + "guildera2"
                + File.pathSeparator + "player"
                + File.pathSeparator + playerName
                + File.pathSeparator);
    }

    public GPlayer getPlayer(String name) {
        for(GPlayer gply : playerList) {
            if(gply.getPlayer().getName().equals(name)) {
                return gply;
            }
        }
        return null;
    }

    public GPlayer getOnlinePlayer(String name) {
        if(findPlayerByName(name, true) != null) {
            return getPlayer(name);
        }
        return null;
    }

    // Only for internal use!
    private Player findPlayerByName(String name, boolean onlineOnly) {
        for(Player ply : main.getServer().getOnlinePlayers()) {
            if(ply.getName().equals(name)) {
                return ply;
            }
        }
        if(onlineOnly) { return null; }
        for(OfflinePlayer ply : main.getServer().getOfflinePlayers()) {
            if(ply.getName().equals(name)) {
                return ply.getPlayer();
            }
        }
        return null;
    }
}
